<template>
	<view class="floor-container">
		<search-box @reset="resetFloor" @search="searchFloor" @add="goAddFloor" placeholder="请输入楼层号" addText="新增楼层"></search-box>
		<floor-list v-if="floorList" @edit="goEditFloor" @delete="goDeleteFloor" :floor-list="floorList"></floor-list>
		<view v-else>
			<van-empty description="暂无楼栋" />
		</view>
		<uni-load-more v-if="floorList && floorList.length >= 15" :status="status" />
	</view>
</template>

<script>
	import FloorList from '../components/floorList.vue'
	import SearchBox from '../components/searchBox.vue'
	
	export default {
		components: {
			SearchBox,
			FloorList
		},
		data() {
			return {
				status: 'more',
				floorList: [],
				params: {
					网格id: null,
					检查点id: null,
					pagination: {
						current: 1,
						pageSize: 20
					},
					sorter: {
						field: "id",
						order: 0
					}
				}
			}
		},
		onLoad(options) {
			this.params.网格id = uni.getStorageSync('gridInfo').所属网格id
			this.params.检查点id = +options.id
		},
		onShow() {
			this.getFloorList()
		},
		onReachBottom() {
			console.log('触发上拉')
			if(this.status !== 'noMore' && this.floorList) {
				this.status = 'loading'
				this.getMoreFloorList()
			}
		},
		methods: {
			resetFloor() {
				this.params.楼层号 = null
				this.getFloorList()
			},
			searchFloor(event) {
				this.params.楼层号 = event
				this.getFloorList()
			},
			goAddFloor() {
				uni.navigateTo({
					url: '/fireGridCheck/addFloor/addFloor?id=' + this.params.检查点id
				})
			},
			goEditFloor(item) {
				uni.navigateTo({
					url: '/fireGridCheck/floorDetail/floorDetail?item=' + JSON.stringify(item) + '&id=' + this.params.id
				})
			},
			getFloorList() {
				this.params.pagination.current = 1
				this.status = 'more'
				this.$http.post('gridSystem/floor/list', this.params).then(res => {
					console.log(res, '楼层')
					if (res.code === 10000) {
						if(res.data.list !== null) {
							this.$common.transformFloor(res.data.list)
						}
						this.floorList = res.data.list
						if(this.floorList && this.floorList.length === res.data.total){
							this.status = 'noMore'
						}
					}
				})
			},
			getMoreFloorList() {
				this.params.pagination.current++
				this.$http.post('gridSystem/floor/list', this.params).then(res => {
					console.log(res)
					if(res.code === 10000){
						if(res.data.list !== null) {
							this.$common.transformFloor(res.data.list)
						}
						this.floorList = this.floorList && this.floorList.concat(res.data.list)
						if(this.floorList.length === res.data.total){
							this.status = 'noMore'
						}
					}
				})
			},
			goDeleteFloor(event) {
				this.$common.showModal('确定要删除该楼层吗', this.deleteFloor, event)
			},
			deleteFloor(obj) {
				this.$http.post('gridSystem/floor/delete', {
					楼层id: obj.id
				}).then(res => {
					if (res.code === 10000) {
						this.floorList.splice(obj.index, 1)
						uni.showToast({
							title: '删除成功'
						})
						// this.getFloorList()
					}else if(res.code === 50012) {
						uni.showToast({
							title: '删除失败,楼层下存在房屋',
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style>

</style>
